package org.zjsru.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.zjsru.pojo.Payment;
import org.zjsru.pojo.PaymentRecord;
import org.zjsru.service.PaymentService;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/payment")
public class PaymentController {

    @Autowired
    private PaymentService paymentService;

    /**
     * 发起支付
     */
    @PostMapping("/pay")
    public Map<String, Object> pay(@RequestParam Long orderId,
                                   @RequestParam Long userId,
                                   @RequestParam String orderNo,
                                   @RequestParam BigDecimal amount) {
        Map<String, Object> result = new HashMap<>();
        Payment payment = paymentService.pay(orderId, userId, orderNo, amount);
        result.put("success", true);
        result.put("payment", payment);
        return result;
    }

    /**
     * 支付回调
     */
    @PostMapping("/callback")
    public Map<String, Object> callback(@RequestParam String paymentNo, @RequestBody String callbackContent) {
        Map<String, Object> result = new HashMap<>();
        boolean success = paymentService.paymentCallback(paymentNo, callbackContent);
        result.put("success", success);
        return result;
    }

    /**
     * 查询支付记录
     */
    @GetMapping("/records/{orderId}")
    public List<PaymentRecord> getRecords(@PathVariable Long orderId) {
        return paymentService.getPaymentRecordsByOrderId(orderId);
    }

    /**
     * 查询支付信息
     */
    @GetMapping("/info/{orderId}")
    public Payment getPaymentInfo(@PathVariable Long orderId) {
        return paymentService.getPaymentByOrderId(orderId);
    }
} 